package com.ceyear.my_idcard;

import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.ceyear.my_idcard.HttpUtils;

import org.apache.http.HttpResponse;
import org.json.JSONException;
import org.json.JSONObject;

public class AliyunAPIHelper {

    // Replace with your own AppCode
    private static final String APP_CODE = "dfa840e41e35455c9adf0552c5590e99";
    private static final String HOST = "https://zidv2.market.alicloudapi.com";
    private static final String PATH = "/idcheck/Post";
    private static final String METHOD = "POST";

    public interface AliyunAPIResponseListener {
        void onSuccess(String response);

        void onFailure(Exception e);
    }

    public static void verifyID(String cardNo, String realName, AliyunAPIResponseListener listener) {
        new NetworkTask().execute(cardNo, realName, listener);
    }

    private static class NetworkTask extends AsyncTask<Object, Void, String> {
        private AliyunAPIResponseListener listener;
        @Override
        protected String doInBackground(Object... params) {
            String cardNo = (String) params[0];
            String realName = (String) params[1];
            listener = (AliyunAPIResponseListener) params[2];

            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", "APPCODE " + APP_CODE);
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, String> querys = new HashMap<>();

            Map<String, String> bodys = new HashMap<>();
            bodys.put("cardNo", cardNo);
            bodys.put("realName", realName);

            try {
                return HttpUtils.doPost(HOST, PATH,headers, querys,bodys);
                //return response.toString(); // Assuming HttpResponse has a useful toString() method
            } catch (IOException e) {
                Log.e("AliyunAPIHelper", "Error making API call", e);
                return null;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != null) {
                // Handle successful response
                Log.d("AliyunAPIHelper", "Response: " + result);
                try {
                    JSONObject jsonResponse = new JSONObject(result);
                    int errorCode = jsonResponse.getInt("error_code");
                    if (errorCode == 0) {
                        listener.onSuccess(result);
                    } else {
                        listener.onFailure(new Exception("API returned error code: " + errorCode));
                    }
                } catch (JSONException e) {
                    listener.onFailure(e);
                }
            } else {
                // Handle failure
                listener.onFailure(new Exception("Request failed"));
                Log.e("AliyunAPIHelper", "Request failed");
            }
        }
    }
}

